package com.admin.controller;


import com.admin.entity.Class;
import com.admin.service.ClassService;
import com.admin.utils.Result;
import com.admin.vo.query.ClassQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author szy
 * @since 2023-02-21
 */
@RestController
@RequestMapping("/api/class")
public class ClassController {

    @Resource
    private ClassService classService;

    /*
    * 查询班级列表
    * */
    @GetMapping("/list")
    public Result list(ClassQueryVo classQueryVo){
        IPage<Class> page = new Page<Class>(classQueryVo.getPageNo(), classQueryVo.getPageSize());
        classService.getClassByPage(page,classQueryVo);
        return Result.ok(page);
    }


    //添加班级方法
    @PostMapping("/add")
    public Result add(@RequestBody Class class1){
        if(classService.save(class1)){
            return Result.ok().message("班级添加成功");
        }
        return Result.error().message("班级添加失败");
    }
    //修改班级方法
    @PutMapping("/update")
    public Result update(@RequestBody Class class1){
        if(classService.updateById(class1)){
            return Result.ok().message("修改班级成功");
        }
        return Result.error().message("修改班级失败");
    }

    //删除班级方法
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        if(classService.removeById(id)){
            return Result.ok().message("删除班级成功");
        }
        return Result.error().message("删除班级失败");
    }
}

